package com.admin.infrastructure.persistence.jdbc;

import com.admin.domain.modle.Member;
import com.admin.domain.repository.MemberRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 会员操作持久层
 *
 * @author LeeJun
 * @version 2.0.0
 * @time 2017-4-18 18:14
 */
@Repository
public class MemberRepositoryJdbc implements MemberRepository {

	@Autowired
	protected JdbcTemplate jdbcTemplate;

	@Override
	public int add(Member member) {
		return jdbcTemplate
				.update("INSERT INTO `member` (`id`,`openId`,`nickName`,`gender`,`city`,`province`,`country`,`language`,`headimgurl`,`isSubscribe`,`subscribeTime`,`remark`)"
						+ " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
						member.getId(), member.getOpenId(),
						member.getNickName(), member.getGender(),
						member.getCity(), member.getProvince(),
						member.getCountry(), member.getLanguage(),
						member.getHeadimgurl(), member.isSubscribe(),
						member.getSubscribeTime(), member.getRemark());
	}

	@Override
	public Member getById(String id) {
		return jdbcTemplate.queryForObject("select * from member where id=?",
				BeanPropertyRowMapper.newInstance(Member.class), id);
	}

	@Override
	public Member getByOpenId(String openId) {
		return jdbcTemplate.queryForObject(
				"select * from member where openId=?",
				BeanPropertyRowMapper.newInstance(Member.class), openId);
	}

	@Override
	public int update(Member member) {
		return 0;
	}

	@Override
	public List<Member> list(String key) {
		if (StringUtils.isEmpty(key)) {
			return jdbcTemplate.query("select * from member",
					BeanPropertyRowMapper.newInstance(Member.class));
		} else {
			return jdbcTemplate.query(
					"select * from member WHERE nickName LIKE ?",
					BeanPropertyRowMapper.newInstance(Member.class), "%" + key
							+ "%");
		}

	}

	@Override
	public int remove(String id) {
		return jdbcTemplate.update("DELETE FROM member WHERE id=?", id);
	}

	@Override
	public Member upsert(Member member) {
		StringBuilder sb = new StringBuilder();
		sb.append("insert into member (id,openId, nickName, gender,headimgurl,province,city,country, isSubscribe, subscribeTime,language, remark,createTime) values ");
		sb.append("(?,?,?,?,?,?,?,?,?,?,?,?,now()) ");
		sb.append("ON DUPLICATE KEY UPDATE ");
		sb.append("nickName=ifnull(values(nickName),nickName),");
		sb.append("gender=ifnull(values(gender),gender),");
		sb.append("headimgurl=ifnull(values(headimgurl),headimgurl),");
		sb.append("province=ifnull(values(province),province),");
		sb.append("city=ifnull(values(city),city),");
		sb.append("country=ifnull(values(country),country),");
		sb.append("isSubscribe=ifnull(values(isSubscribe),isSubscribe),");
		sb.append("subscribeTime=ifnull(values(subscribeTime),subscribeTime),");
		sb.append("language=ifnull(values(language),language),");
		sb.append("remark=ifnull(values(remark),remark)");
		jdbcTemplate.update(sb.toString(), member.getId(), member.getOpenId(),
				member.getNickName(), member.getGender(),
				member.getHeadimgurl(), member.getProvince(), member.getCity(),
				member.getCountry(), member.isSubscribe(),
				member.getSubscribeTime(), member.getLanguage(),
				member.getRemark());
		return getByOpenId(member.getOpenId());
	}
}
